<?php

function add_al_guestbook_admin_menu($admin_menu) {
	array_push($admin_menu, array('Guestbook'));
	return $admin_menu;
}

function add_al_guestbook_admin_submenu($admin_sub_menu) {
	$admin_sub_menu['Guestbook'][] = array('Manage Guestbook', 'manage_guestbook', 'action'=>array('read and manage'));
	$admin_sub_menu['Guestbook'][] = array('', 'show_guestbook');
	return $admin_sub_menu;
}

function add_al_guestbook_plugin_dir($controller_name) {
	$actions = array('manage_guestbook');
	$result = $controller_name;
	if (in_array($controller_name, $actions)) {
		$result = 'al_guestbook';
	}
	return $result;
}

function add_al_guestbook_language($languages) {
    $languages['Guestbook'] = '留言板';
    $languages['Manage Guestbook'] = '留言板管理';
    $languages['Show Guestbook'] = '留言查看';
    return $languages;
}

function get_all_guestbook($page, $type = null, $url = null) {
    global $aldb;
    arlicle_components('page');
    $perpage = 20;
    $condition = '';
    if (empty($type) || $type == 'checked') {
        $condition = " WHERE `status`=1 AND `parent`='0' ";
    } else if ($type == 'all') {
        $condition = " WHERE `parent`='0' ";
    }
    $count = $aldb->get_var("SELECT COUNT(id) FROM $aldb->guestbook $condition");

    # get the start of the page
    $total = ceil($count/$perpage);
    $page = intval($page);
    if (empty($page) || $page < 1) {
        $page = 1;
    } else if ($page > $total) {
        $page = $total;
    }
    $start = ($page -1) * $perpage;

    $posts->page = $page;
    $posts->results =  $aldb->get_results("SELECT * FROM $aldb->guestbook $condition ORDER BY id DESC LIMIT $start, $perpage");
    $posts->pagelink = get_pagelink($url, $page, $count, $perpage,$total);
    return $posts;
}

function show_al_guestbook_status($status) {
    $title = '';
    switch($status) {
        case 1:
            $title = '已审核';
            break;
       case 0:
       default:
            $title = '待审核';
            break;
    }
    return $title;
}

function get_reply($id) {
    global $aldb;
    return $aldb->get_row("SELECT * FROM $aldb->guestbook WHERE parent='$id'");
}
?>
